<?php

error_reporting(E_STRICT | E_ALL);
date_default_timezone_set('America/New_York');
ini_set('display_errors', 1);
set_time_limit(60*60);
ini_set('memory_limit', '128M');

$g_start = microtime(true);
require_once "config.php";
require_once dirname(__FILE__). '/util/connection.class.php';
$dbconn = Connection::getInstance();

$regUsersId = getRegUsers($dbconn);
if(count($regUsersId)>0){
	$usersInfo = getUsersInfo($regUsersId, $dbconn);
	partnerSub($usersInfo, $dbconn);
//	setStatusSub($regUsersId, $dbconn);
}

echo "\n". (microtime(true)-$g_start) . " seconds\n";



function getRegUsers(Connection $db)
{
	$sql = " SELECT user_id FROM partner_subscriber WHERE status='reg' limit 50";
	try{
		$result = $db->query($sql);
		if(!empty($result)){
			foreach($result as $k=>$v){
				$result[$k] = $v['user_id'];
			}	
		}
		return $result;
	}catch(Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}

function getUsersInfo($usersId, Connection $db)
{
	$sql = " SELECT u.*, s.domain AS siteurl FROM users u LEFT JOIN sites s ON u.site_id=s.id WHERE u.id IN ( " . implode(', ', $usersId) . " ) ";
	try{
		$result = $db->query($sql);
		return $result;
	}catch (Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}

function partnerSub($usersInfo, Connection $db)
{
	foreach($usersInfo as $user){
		list($y, $m, $d) = explode('-', $user['birthday']);
		
		$e = trim($user['email']);
		$f = trim($user['firstname']);
		$l = trim($user['lastname']);
		$a = '';//$user['address1'];
		$c = '';//$user['city'];
		$st = '';//$user['state'];
		$zip = '';//$user['zipcode'];
		$cc = '';
		$p = '';//$user['phone'];
		$dob = $user['birthday'];
		$yob  =  $y;
		$age  =  date('Y') - $yob;
		$g = $user['gender'];
		$ad  =  date('Y-m-d', $user['created']);
		$siteurl = $user['siteurl'];
		$ip  =  long2ip($user['ip']);//$_SERVER['REMOTE_ADDR'];
		$cid = '12418';
		
		$url = "http://feed.globalwidemedia.com/feed.php?e=$e&f=$f&l=$l&a=$a&c=$c&st=$st&zip=$zip&cc=$cc&p=$p&dob=$dob&yob=$yob&age=$age&g=$g&ad=$ad&siteurl=$siteurl&ip=$ip&cid=$cid";
		$content = file_get_contents($url);
		//var_dump($content);
//		echo $url . "\n";
		if(strtolower(trim($content)) == 'success'){
			setStatusSub(array( $user['id'] ), 'sub', $db);
		}else{
			setStatusSub(array( $user['id'] ), 'sub_failed', $db);
		}
	}
}

function setStatusSub($usersId, $status, Connection $db)
{
	$sql = " UPDATE partner_subscriber SET status=?, sub_date=? WHERE user_id IN ( " . implode(', ', $usersId) . " ) ";
	try{
		$result = $db->execute($sql, array( $status, date('Y-m-d') ));
		return $result;
	}catch (Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}

	
	
	
	
	
	
	